#include "test_webclient.hpp"

/****************************************************
*       System dependencies
****************************************************/
#include <sstream>

#include <cppunit/TestCaller.h>

/****************************************************
*       Namespaces
****************************************************/
using namespace std;
using namespace CppUnit;

void WebClientTest::setUp() {
    wc = new WebClient(WebClient::Synchronous);
}

void WebClientTest::tearDown() {
    delete wc;
}

void WebClientTest::testGet()
{   
    int status;
    stringstream *output = new stringstream(stringstream::in | stringstream::out);

    status = wc->get("http://www.google.com/search?q=socialbox", output);
    output->seekg(0, ios::end);
    CPPUNIT_ASSERT(output->tellg() >= 0);
    CPPUNIT_ASSERT(status == 302);

    delete output;
}

void WebClientTest::testPost()
{
    int status;
    stringstream *output = new stringstream(stringstream::in | stringstream::out);
    list<string> args;
    args.push_back("userComment=AValueToDisplay");
    status = wc->post("http://www.javasonics.com/listenup/php_test/test_post.php", args, output);
    output->seekg(0, ios::end);
    CPPUNIT_ASSERT(output->tellg() >= 0);
    CPPUNIT_ASSERT(status == 200);

    delete output;
}

void WebClientTest::testCookies()
{
    int status;
    stringstream *output = new stringstream(stringstream::in | stringstream::out);
	Poco::HashMap<std::string, std::string> _cookies;
	_cookies["SSID"] = "00000000011111";
	wc->addCookies(_cookies);
    status = wc->get("http://www.google.com/search?q=socialbox", output);
    output->seekg(0, ios::end);
    CPPUNIT_ASSERT(output->tellg() >= 0);
    CPPUNIT_ASSERT(status == 302);

    delete output;
}

CppUnit::Test* WebClientTest::suite() {
    TestSuite *pSuite = new TestSuite("WebClient");
    pSuite->addTest( new CppUnit::TestCaller<WebClientTest> ("testGet", &WebClientTest::testGet));
    pSuite->addTest( new CppUnit::TestCaller<WebClientTest> ("testPost", &WebClientTest::testPost));
    pSuite->addTest( new CppUnit::TestCaller<WebClientTest> ("testCookies", &WebClientTest::testCookies));
    return pSuite;
}

